#include "includes.h"

#define INITTAB_TEST "/etc/inittab_test"
#define INITTAB_BACK "/etc/inittab_back"


typedef struct
{
	long time;
//	long power;
	char sample_name[32];
	char sample_power;
	char reserve[3];
}S_PACKET_AUDIO_DATA, *PS_PACKET_AUDIO_DATA;


void *deal(void *arg)
{
	char recv_buf[1024];
	char buff[512];
	unsigned char task;
	PS_PACKET_DATA pstPacket;
	PS_PACKET_AUDIO_DATA pstPacket_audio;
	PS_AUDIO_UNIT pstPacket_unit;
	S_AUDIO_PAR stAudio_par;
	unsigned int array_size;
	short *sample_a;
	char power;
	int ret;
	int rlen;
	int cnt, i;
	FILE *fp;
	
	time_t asy_time;

	
	//S_AUDIO_UNIT stAudio_unit;
	while(1)
	{
		rlen = task_buff_read(recv_buf, sizeof(recv_buf));
		set_sys_stat(STAT_SYS_BUSY);
		pstPacket = (PS_PACKET_DATA)recv_buf;
		task = pstPacket->content;
		switch(task)
		{
			case CONTENT_AUDIO_OUT:
				pstPacket_audio = (PS_PACKET_AUDIO_DATA)pstPacket->data;
				audio_write(pstPacket_audio->sample_name);
				break;
			case CONTENT_AUDIO_IN:
				pstPacket_audio = (PS_PACKET_AUDIO_DATA)pstPacket->data;
				power = pstPacket_audio->sample_power;
				power = min(31, power);
				array_size = 1<<power;
				sample_a = (short *)Malloc(array_size*sizeof(short));
				bzero(sample_a, array_size*sizeof(short));

				Sleep_us(2000000);
				audio_read(sample_a, power, 1);
				ret = audio_analyse(&stAudio_par, sample_a, power);
				set_sys_stat(STAT_SYS_MIDDLE);
				if (task_buff_read_timeout(recv_buf, sizeof(recv_buf), 10) == -1)
				{
					set_sys_stat(STAT_SYS_FREE);
					break;
				}
				set_sys_stat(STAT_SYS_BUSY);
				pstPacket = (PS_PACKET_DATA)recv_buf;
				if (pstPacket->content == CONTENT_AUDIO_IN)
				{
					if (ret != 0)
						send_ack_data_packet(ERR_CODE_ANALYSIS, pstPacket, get_cur_addr(), NULL, 0);
					else
						send_ack_data_packet(ERR_CODE_OK, pstPacket, get_cur_addr(), (char *)&stAudio_par, sizeof(S_AUDIO_PAR));
				}
				free(sample_a);
				break;
			case CONTENT_NOISE_CAL:
				printf("###############the noise cal\n");
				pstPacket_audio = (PS_PACKET_AUDIO_DATA)pstPacket->data;
				power = pstPacket_audio->sample_power;
				power = min(31, power);
				Sleep_us(500000);
				cal_noise(power);				
				break;
			case CONTENT_NOISE_PLAY:
				printf("###############the noise play\n");
				audio_noise_play();
				break;
			case CONTENT_RS485_TEST:
				ret = uart_rs485_read_timeout(buff, sizeof(buff), 10);
				if (ret != -1)
				{
					buff[ret] = 0;
					printf("recv buff is %s", buff);
					uart_rs485_write(buff, ret);
				}
				break;
			case CONTENT_ENTER_NORMAL:
				system("mv /etc/inittab_back /etc/inittab");
				system("reboot");
				break;
			case CONTENT_AUDIO_PAR:
				pstPacket_unit = (PS_AUDIO_UNIT)pstPacket->data;
				if (rlen % sizeof(S_AUDIO_UNIT) == 0)
				{
					cnt = rlen/sizeof(S_AUDIO_UNIT);
					fp = Fopen("/Skuo/audio_result.txt", "w+");
					fprintf(fp, "********audio test result*******\n\n\n");
					for (i=0; i<cnt; i++)
					{
						fprintf(fp, "TEST FILE:%s\n", pstPacket_unit->name);
						fprintf(fp, "SOURCE FREQ:%d\n", pstPacket_unit->freq);
						fprintf(fp, "MIC FREQUENCY:%d\n", pstPacket_unit->in_par.freq);
						fprintf(fp, "MIC FREQUENCY RESPONSE:%f\n", pstPacket_unit->in_par.fr);
						fprintf(fp, "MIC SNR:%f\n", pstPacket_unit->in_par.snr);
						fprintf(fp, "MIC THD:%f\n", pstPacket_unit->in_par.thd);
						fprintf(fp, "SPEAK FREQUENCY:%d\n", pstPacket_unit->out_par.freq);
						fprintf(fp, "SPEAK FREQUENCY RESPONSE:%f\n", pstPacket_unit->out_par.fr);
						fprintf(fp, "SPEAK SNR:%f\n", pstPacket_unit->out_par.snr);
						fprintf(fp, "SPEAK THD:%f\n", pstPacket_unit->out_par.thd);
						fprintf(fp, "\n\n\n");
						pstPacket_unit++;
					}
					Fclose(fp);
				}
				
				break;
			case CONTENT_TIME_SYN:
				memcpy(&asy_time, pstPacket->data, sizeof(time_t));
				stime(&asy_time);
				break;
			default:
				break;
		}

		set_sys_stat(STAT_SYS_FREE);
	}
}

